<template>
	<view class="growupPage">
		<view class="growupContent">
			<bar title="热门推荐"></bar>
			<view class="liveBroadcast">
				<view class="liveBroadcastList" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
					<u--image :src="baseUrl+'/sys/common/static/'+item.images" shape="square" mode="aspectFill"
						width="284rpx" height="284rpx" radius="12rpx">
					</u--image>
					<view class="title">{{item.curriculumName}}</view>
				</view>
			</view>
			<!-- 轮播 -->
			<view class="communityInfoSwiper">
				<u-swiper :list="list1" imgMode="aspectFill" indicator indicatorMode="dot" circular height="200rpx"
					radius="20rpx" @click="goLive"></u-swiper>
			</view>
			<bar title="学习视频" :isLink="true" url="/pages/studyList/studyList"></bar>
			<view class="studyMovie">
				<view class="studyList" v-for="(item,index) in list2" :key="index" @click="goDetail(item)">
					<view class="image">
						<u--image :src="baseUrl+'/'+item.images" shape="square" mode="aspectFill" width="320rpx"
							height="226rpx" radius="8rpx 8rpx 0 0">
						</u--image>
						<view class="movieStatus">
							<u--image src="/static/growup/1.png" shape="square" mode="aspectFill" width="88rpx"
								height="88rpx" radius="12rpx">
							</u--image>
						</view>
					</view>
					<view class="studyInfo">
						<view class="title">{{item.curriculumName}}</view>
						<view class="statics">
							<view>{{item.curriculumAuthor}}</view>
							<view class="total">累计：{{item.views||0}}</view>
						</view>
					</view>
				</view>
			</view>
			<bar title="文档资料" :isLink="true" url="/pages/documentList/documentList"></bar>
			<view class="documentContent">
				<view class="documentList" v-for="(item,index) in list3" :key="index">
					<view class="status" v-if="item.isExchange">
						<u--image src="/static/vOnTrial/1.png" shape="square" mode="aspectFill" width="82rpx"
							height="82rpx">
						</u--image>
					</view>
					<view class="image">
						<u--image :src="getImgUrl(item.uploadUrl).imageUrl" shape="square" mode="aspectFill" width="116rpx"
							height="116rpx">
						</u--image>
					</view>
					<view class="documentInfo">
						<view class="title">{{item.curriculumName}}</view>
						<view class="info">
							<view class="author">{{item.curriculumAuthor}}</view>
							<view class="createtime">{{item.createTime}}</view>
						</view>
						<view class="cost">兑换：{{item.integral||0}}积分</view>
					</view>
					<view class="documentBtn" :class="item.isExchange?'color0':'color2'" @click="doChange(item)" v-if="isSendArr.indexOf(item.id)==-1">
						{{item.isExchange?'发送邮箱':'积分兑换'}}</view>
					<view class="documentBtn color1"  @click="doClip(item)" v-else>复制链接</view>
				</view>
			</view>
			<u-popup :show="showExchange" :round="10" mode="bottom" @close="doClose">
				<view class="exchangePanel">
					<view class="title">积分兑换</view>
					<view class="cost">{{exchangeInfo.integral}}
						<view class="unit">积分</view>
					</view>
					<view class="detail">
						<view class="userScore" v-if="exchangeInfo.canExchange">当前积分{{userScore}}</view>
						<view class="userScore" v-if="!exchangeInfo.canExchange">当前积分不足，请到任务中心获取积分</view>
						<view class="vBtn" @click="doExchange(exchangeInfo)">{{exchangeInfo.btnText}}</view>
					</view>
				</view>
			</u-popup>
		</view>
		<tabbar :current="3"></tabbar>
	</view>
</template>

<script>
	import Tabbar from "@/components/tabbar.vue";
	import Bar from "@/components/bar.vue";
	export default {
		components: {
			Tabbar,
			Bar
		},
		data() {
			return {
				baseUrl: this.$api.baseUrl,
				showExchange: false,
				userScore: "",
				myEmail: "",
				exchangeInfo: {
					cost: "0",
					canExchange: false,
					btnText: "做任务"
				},
				list: [],
				list1: [],
				list1Data: [],
				list2: [],
				list3: [],
				userId: "",
				isLoad: false,
				isSendArr: [],
				colorIndex: 0
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
					})
					this.initData();
				}
			})
		},
		onShow() {
			this.refreshUser()
		},
		onPullDownRefresh() {
			this.initData();
		},
		methods: {
			initData() {
				Promise.all([
					this.$request.get(this.$api.hotList, {
						pageNo: 1,
						pageSize: 5,
						userId: this.userId
					}),
					this.$request.get(this.$api.studyList, {
						pageNo: 1,
						pageSize: 4,
						type: 1,
						userId: this.userId
					}),
					this.$request.get(this.$api.studyList, {
						pageNo: 1,
						pageSize: 5,
						type: 2,
						userId: this.userId
					}),
					this.$request.get(this.$api.getBanner, {
						type: 3
					}),
				]).then(res => {
					this.list = res[0].result.records;
					this.list2 = res[1].result.records;
					this.list3 = res[2].result.records;
					this.list1 = [];
					this.list1Data = res[3].result;
					res[3].result.map(item => {
						this.list1.push(this.$api.baseUrl + "/sys/common/static/" + item.url)
					})
					this.isLoad = true;
					uni.stopPullDownRefresh()
					uni.hideLoading()
				})
			},
			refreshUser() {
				if (this.isLoad) {
					console.log("刷新邮箱和积分数据")
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						uni.setStorage({
							key: "userInfo",
							data: result.result
						})
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
						uni.hideLoading()
					})
					this.initData();
				}
			},
			getDocument() {
				this.$request.get(this.$api.studyList, {
					pageNo: 1,
					pageSize: 5,
					type: 2,
					userId: this.userId
				}).then(res => {
					this.list3 = res.result.records;
					uni.showToast({
						icon: "success",
						title: "兑换成功"
					})
					this.showExchange = false;
					uni.hideLoading()
				})
			},
			getImgUrl(item) {
				let that = this
				let arr = item.split(".");
				let type = arr[arr.length - 1];
				let obj = {};
				switch (type) {
					case "zip":
						obj.imageUrl = '/static/growup/2.png'
						// obj.colorIndex = 0
						break;
					case "doc":
						obj.imageUrl = '/static/growup/3.png'
						// obj.colorIndex = 2
						break;
					case "docx":
						obj.imageUrl = '/static/growup/3.png'
						// obj.colorIndex = 2
						break;
					case "pdf":
						obj.imageUrl = '/static/growup/4.png'
						// obj.colorIndex = 1
						break;
					case "xls":
						obj.imageUrl = '/static/growup/5.png'
						// obj.colorIndex = 0
						break;
					case "xlsx":
						obj.imageUrl = '/static/growup/5.png'
						// obj.colorIndex = 0
						break;
					case "ppt":
						obj.imageUrl = '/static/growup/6.png'
						// obj.colorIndex = 0
						break;
					default:
						obj.imageUrl = '/static/growup/8.png'
						// obj.colorIndex = 0
						break;
				}
				return obj
			},
			goLive(index) {
				if (this.list1Data[index].link) {
					uni.navigateTo({
						url: this.list1Data[index].link+"?id="+this.list1Data[index].id
					})
				}
			},
			doClose() {
				this.showExchange = false
			},
			doClip(item) {
				uni.setClipboardData({
					data: this.baseUrl + '/sys/common/static/' + item.uploadUrl,
					success: function() {
						uni.showToast({
							title: "复制成功",
							icon: "success"
						})
					}
				});
			},
			doChange(item) {
				if (item.isExchange) {
					if (this.myEmail) {
						this.isSendArr.push(item.id);
						this.$request.get(this.$api.sendEmailFile, {
							email: this.myEmail,
							title: item.curriculumName,
							content: "点击附件下载文件",
							filepath: this.baseUrl + '/sys/common/static/' + item.uploadUrl,
							filename: item.uploadUrl
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '您还没有绑定邮箱，请去个人中心绑定邮箱账号',
							confirmText: "前去绑定",
							cancelText: "放弃领取",
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/myInfo/myInfo"
									})
								} else if (res.cancel) {
									uni.showToast({
										icon: "none",
										title: "已取消发送"
									})
								}

							}
						})
					}

				} else {
					this.showExchange = true;
					if (item.integral > this.userScore) {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = false;
						this.exchangeInfo.btnText = "做任务"
						this.exchangeInfo.integral = item.integral
					} else {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = true;
						this.exchangeInfo.btnText = "立即兑换"
						this.exchangeInfo.integral = item.integral
					}
				}
			},
			doExchange(item) {
				if (item.integral > this.userScore) {
					uni.navigateTo({
						url: "/pages/taskCenter/taskCenter"
					})
				} else {
					this.$request.post(this.$api.doExchange, {
						integral: item.integral,
						otherId: item.id,
						userId: this.userId,
						exchangeType: 2
					}).then(res => {
						this.getDocument();
						this.refreshUser();
						uni.hideLoading()
					})
				}
			},
			goDetail(item) {
				uni.navigateTo({
					url: '/pages/studyDetail/studyDetail?id=' + item.id
				})
			}
		}
	}
</script>

<style lang="scss">
	.growupPage {
		height: calc(100vh - 100rpx);
		overflow-y: auto;

		.growupContent {
			.liveBroadcast {
				display: flex;
				justify-content: flex-start;
				flex-wrap: nowrap;
				width: calc(100% - 80rpx);
				overflow-x: auto;
				padding: 0 40rpx;

				.liveBroadcastList {
					position: relative;
					margin-right: 20rpx;

					.title {
						position: absolute;
						left: 0;
						top: 0;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
						font-size: 28rpx;
						line-height: 28rpx;
						height: 28rpx;
						padding: 26rpx 24rpx 0;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
						width: calc(100% -48rpx);
					}
				}
			}

			.communityInfoSwiper {
				padding: 30rpx 40rpx 10rpx;
				width: calc(100% - 80rpx);
			}

			.studyMovie {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				padding: 0 40rpx;

				.studyList {
					display: flex;
					flex-direction: column;
					width: 320rpx;
					background-color: #fff;
					margin-bottom: 30rpx;
					border-radius: 8rpx;

					.image {
						position: relative;

						.movieStatus {
							position: absolute;
							left: calc(50% - 44rpx);
							top: calc(50% - 44rpx);
						}
					}

					.studyInfo {
						padding: 24rpx 24rpx 20rpx;

						.title {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
							font-size: 26rpx;
							line-height: 36rpx;
							height: 72rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
						}

						.statics {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							display: flex;
							flex-wrap: nowrap;
							justify-content: space-between;
							margin-top: 20rpx;

							.total {
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #999999;
							}
						}
					}
				}
			}

			.documentContent {
				padding: 10rpx 40rpx 40rpx;
				display: flex;
				justify-content: flex-start;
				flex-direction: column;

				.documentList {
					background: #FFFFFF;
					box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
					border-radius: 20rpx;
					margin-bottom: 20rpx;
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					align-items: center;
					padding: 30rpx 26rpx;
					position: relative;

					.status {
						position: absolute;
						top: 0;
						left: 0;
					}

					.image {
						margin-right: 26rpx;
					}

					.documentInfo {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						height: 126rpx;

						.title {
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							font-size: 26rpx;
						}

						.info {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							display: flex;
							flex-wrap: nowrap;

							.author {
								position: relative;
								padding-right: 8rpx;

								&:after {
									content: "";
									display: inline-block;
									height: 22rpx;
									width: 2rpx;
									background-color: #BDBDBDFF;
									position: absolute;
									right: 0;
									top: calc(50% - 11rpx);
								}
							}

							.createtime {
								padding-left: 8rpx;
							}
						}

						.cost {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #EC6C2F;
						}
					}

					.documentBtn {
						position: absolute;
						right: 20rpx;
						background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
						border-radius: 34rpx;
						height: 50rpx;
						width: 136rpx;
						line-height: 50rpx;
						font-size: 24rpx;
						color: #fff;
						text-align: center;
					}
					.color0 {
						background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					}
					.color1 {
						background: linear-gradient(180deg, #F3D748 0%, #EF7B17 100%);
					}
					.color2 {
						background: linear-gradient(180deg, #F79037 0%, #F24929 100%);
					}
				}
			}
		}

		.exchangePanel {
			display: flex;
			text-align: center;
			flex-direction: column;

			.title {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				font-size: 28rpx;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;
			}

			.cost {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				font-size: 46rpx;
				color: #EC6C2FFF;
				display: flex;
				flex-wrap: nowrap;
				justify-content: center;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;

				.unit {
					font-size: 24rpx;
					position: relative;
					top: 6rpx;
					left: 2rpx;
					color: #333333FF;
				}
			}

			.detail {
				line-height: 100rpx;
				display: flex;
				align-items: center;
				flex-direction: column;
				padding-bottom: 40rpx;

				.userScore {
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 30rpx;
				}

				.vBtn {
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					border-radius: 48rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					font-size: 34rpx;
					height: 96rpx;
					width: 680rpx;
				}
			}
		}
	}
</style>
